package com.cn.sl.server.api.system.department.model.dto

import com.cn.sl.server.api.system.department.model.entity.DepartmentEntity
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.validation.constraints.NotBlank
import jakarta.validation.constraints.NotNull

@Schema(description = "添加处室")
data class EditDepartmentParam(
    @field:NotNull(message = "处室主键不能为空")
    @field:Schema(description = "主键")
    var id: Long?,

    @field:Schema(description = "父级id")
    var parentId: Long?,
    /**
     * 名称
     */
    @field:NotBlank(message = "处室名称不能为空")
    @field:Schema(description = "名称")
    val name: String?,

    /**
     * 说明
     */
    @field:Schema(description = "说明")
    val memo: String?,

    /**
     * 排序
     */
    @field:Schema(description = "排序")
    val sort: Int?,
) {
    fun toDto(): EditDepartmentDto {
        return EditDepartmentDto(
            id = this.id!!,
            parentId = this.parentId,
            name = this.name!!,
            memo = this.memo,
            sort = this.sort
        )
    }
}

data class EditDepartmentDto(
    var id: Long,
    var parentId: Long?,
    /**
     * 名称
     */
    val name: String,

    /**
     * 说明
     */
    val memo: String?,

    /**
     * 排序
     */
    val sort: Int?,
) {
    fun merge(entity: DepartmentEntity, parentEntity: DepartmentEntity?): DepartmentEntity {
        val namePath = arrayListOf<String>()
        if (parentEntity != null && parentEntity.namePath.isNotEmpty()) {
            namePath.addAll(parentEntity.namePath)
        }
        namePath.add(this.name)
        return entity.apply {
            this.parentId = this@EditDepartmentDto.parentId
            this.name = this@EditDepartmentDto.name
            this.memo = this@EditDepartmentDto.memo
            this.sort = this@EditDepartmentDto.sort
            this.namePath = namePath
        }
    }
}